The development of ActiveX, Java, and other Internet programming solutions has brought into vogue the idea of software components. Instead of designing computer software as a large, self-contained tool that performs a wide range of tasks, software is broken into smaller modules called components. These components are created to accomplish a specific task, and they are designed to be generic enough to work with a wide range of software. The goal of component-based programming is to make software design simpler, cheaper, faster, and more reusable.
Components make the job of software development more manageable, under the premise that it would be easier to build a fence from a stack of wooden slats, nails, and posts than it would be if someone handed you an ax and pointed to a nearby forest.
ActiveX is the latest and greatest incarnation of the Component Object Model, a standard for software development that Microsoft has developed in the past five years. In this chapter, you'll learn about the Component Object Model and some other advanced topics in regard to ActiveX and VBScript.
ActiveX is built on the Component Object Model, often abbreviated as COM. Because ActiveX is a refinement of the existing COM standard, thousands of programmers familiar with COM can learn ActiveX much more easily.
COM establishes a standard, structured way for programs on the same desktop to share software components. A component can be set up to perform a task, and any other software that wants to use the component can do so by using methods described in the COM. ActiveX extends this system by making it possible to share components across the Internet.
Software components-also called objects-present a different way to organize a computer program. Most software that is developed without components puts all of its functionality into a single, self-contained program or set of programs. Figure 21.1 shows NerdStar, a fictional example of a word processing program developed without using components.
Figure 21.1 : A program with four self-contained functions and no parts using COM.
NerdStar is a self-contained program that does all of its own work. It has functions to handle text editing, spell checking, file handling, and document conversion. If NerdStar needs to accomplish one of these tasks, it calls upon its own functions.
One of the biggest reasons to develop a program this way is because everything is fully compatible. Every part of NerdStar works with every other part (or at least it should!), because each part was designed in conjunction with the rest.
Most software that has been developed in the past 20 years is like NerdStar in its approach, containing the functions that it needs inside a single program or set of programs.
This approach has been highly successful for many years, but it can prove costly when software needs to be improved, modified, or used to create other software.
After many years of success with NerdStar, the developers invent a new spell-checking program, called Spellvis, that is faster and more comprehensive than the leading brand. The decision is made to replace NerdStar's spell-checking routine by putting Spellvis inside a new version of NerdStar. This is not necessarily an easy task.
The difficulty arises because NerdStar's original spell-checking function is thoroughly entwined with the other parts of NerdStar. In a program that is composed of thousands of lines of code, it will be difficult and time-consuming to find all the lines that will be affected if the original spell-checking function is removed. The rest of NerdStar also must be changed in order to work with the Spellvis code inside it, because Spellvis might not operate in a similar manner to the original spell checker.
In order to make NerdStar easier to maintain, expand, and use with other software, it is rewritten using the Component Object Model. The newly christened NerdStar 95 breaks its text editing, file handling, and document conversion functions into their own components, and it also adds Spellvis as its own component. Figure 21.2 shows NerdStar 95, the new version of the word processing program designed with the Component Object Model. All of its functions have been developed as components (shown here as plugs), and the main NerdStar 95 program is a series of sockets. Each component has the job of accomplishing a specific task or set of tasks. The main job of the central NerdStar 95 program is to enable the components to communicate with each other.
Figure 21.2 : A program with all of its components using COM.
Using Figure 21.2 as a reference, the Component Object Model is the standard that defines what a plug is, what a socket is, and how the two connect to each other. When a component follows the standard and a program follows the standard, they can be used together.
COM is a comprehensive specification, but it does not add any overhead to software. There is no COM interpreter or any kind of controlling routine that must be used between the component and the program.
Developing software using COM is a significant undertaking, but NerdStar 95's programmers have an immediate benefit in regard to Spellvis. The company also has introduced Pagerrific, a Web page editor that can be used to create, modify, and view HTML documents.
Pagerrific has its own special text editing, file handling, and document conversion functions, but the company wants to add Spellvis to this new program. Using the COM model, the same Spellvis component used with NerdStar 95 can be plugged into Pagerrific.
Figure 21.3 shows Pagerrific, a program that uses COM for Spellvis and has self-contained functions for the rest of its tasks.
Figure 21.3 : A program with three self-contained functions and a Spellvis component using COM.
Because they use COM, the NerdStar 95 developers will benefit from the following:
These benefits have been discussed in relation to COM, because it is the underlying structure of ActiveX, as you will see in the following section. However, other solutions have been offered for the development of component software. Many of these are focused on the programming language, unlike the Component Object Model, which focuses on the executable programs and can be achieved with many languages. As an example of a component system that differs from COM, object-oriented programming languages such as Java can be used to divide software into self-contained components that can be plugged into other programs as needed.
Although software components and object-oriented programming have reached a new peak in popularity, ActiveX is the product of years of development at Microsoft. In some ways, it can be considered the fourth step in Microsoft's shift toward component software development.
The four steps Microsoft took toward ActiveX are as follows:
COM software components are usable across different languages; a component written with one language can be used in software written with another language. ActiveX components, also called controls, can be used by programmers writing with Microsoft Visual Basic, Micro Focus Visual Object COBOL, PowerBuilder, and other popular development tools. COM has been used as the underlying foundation in applications to offer 3D graphics, transaction processing, and compound documents. This last area is one that you might have encountered with word processing software, as shown in the previous NerdStar example.
One of the areas in which components have been used to great advantage is in office suite software, which offers a word processor, spreadsheet, and other software as a unified set-or suite-of programs. For instance, COM can be used to put a functional spreadsheet inside a text document. The only new programming required in the word processing software would be to plug in the spreadsheet's component.
The COM standard was pioneered by Microsoft, and its progress has largely been at the initiative of the company. However, Microsoft announced recently that it was releasing management of COM, the related Distributed Component Object Model, and ActiveX to an international standards body-the ActiveX Working Group.
The standards group-which consists of Microsoft customers, software vendors, and hardware producers-will be in charge of standardizing these tools and fostering future development. You can find out more about the group at the following Web site:
http://www.activex.org/
The move was made to provide evidence of Microsoft's support for open Internet standards. It could give ActiveX the same kind of push that Java received when Sun Microsystems made the source code of the language publicly available. COM is an open standard, documented from top to bottom for the use of software vendors and other interested parties.
The Component Object Model specification includes a set of standard Application Programming Interfaces (APIs) and network protocols. Software components must follow a standard in binary form, but they can be created with programming languages that can use these APIs to produce components in that binary form.
One advantage of a system such as COM is that it encourages the development of objects that can be used by other programmers. If an application developer needs a special graphics conversion function and a software component is available that can accomplish the task, the developer can plug in the component and avoid the hours required to write new code.
The author of the graphics conversion utility can make the component available easily in binary form, which removes the significant difficulty of making source code available to developers. The focus would be in the right place-developing and enhancing the graphics conversion component-instead of protecting the code from being stolen or used incorrectly.
As shown in ActiveX's use of the OBJECT tag, version control is also possible so that new components are only installed if needed. The following is an example of a CODE attribute in an OBJECT tag:
CODE=http://www.ibfake.com/Spellvis.ocx#Version=3
When an ActiveX-enabled browser encounters an attribute with a version declared in the HTML code, its component download mechanism only retrieves and installs the component if its version is newer than versions the user already has. If no version is included in the CODE attribute, the presence of any version of the component stops it from being downloaded again.
A new wrinkle on the Component Object Model and ActiveX system is the Distributed Component Object Model. This was introduced by Microsoft as a way for software components to communicate with other components across both the Internet and intranets. Described as "COM with a longer wire" by Microsoft, DCOM is an extension of COM for use on a broader scale of multi-user applications. During an early stage of development, DCOM was called Network OLE.
As an ActiveX technology, DCOM provides native support for Internet protocols such as TCP/IP and HTTP, and for the Java language. Because DCOM is language-neutral, Java applets can use DCOM to communicate with each other and with ActiveX controls.
Although COM has been described as an approach that is different from object-oriented programming (OOP), a component system such as COM embodies the same principles. An object, according to OOP terminology, is software that includes two things: functions that indicate how the object will behave, and data that is used to perform those functions. When you combine data and programming to do something with the data, an object contains everything it needs to perform a task. Because of this, when you use an object, you can focus on what it does, not how it does it.
This is called encapsulation. The users of an object don't need-and don't have access to-the internal structure of an object. They can focus on how the object behaves. Referring back to the NerdStar example, a programmer who wants to use the Spellvis component only needs to know how to make it spell check a document.
COM is a system of communication between an object-a component, in other words-and anything that wants to work with the object. Using COM, this communication is done using a request called an interface. A COM interface can be thought of as a contract between two pieces of software. By creating such a strong standard, COM and descendants such as ActiveX provide a method for software to operate together on a large scale.
A lot of Microsoft's Internet strategy involves adapting the company's existing solutions to the Internet. The contents of this book are full evidence of this: VBScript is based on the highly successful Visual Basic programming language, and ActiveX is a successor to OCX development. Cabinet files (also called Cab files) are another example of Microsoft adapting one of its tools for the Net.
Cabinet files are a way to archive Java applets or ActiveX controls into a single file. The Cabinet file archive is compressed into a single file, and it can be used to hold all programs, images, and other files that are used by an applet or ActiveX control.
Also, Cab files can be signed using the same code-signing system as ActiveX controls. This is true of Java files in addition to ActiveX controls, providing an extra level of security for Java programs on the World Wide Web.
The Cab file format is based on Lempel-Ziv compression, one of the popular compression systems, and Microsoft has made a set of command-line tools available for use with the Cabinet archival system. The set of tools is called CABARC, the Cabinet Archiver.
The following Web page, located in the Site Builder section of Microsoft's site, is the current address where Cab tools can be downloaded:
http://www.microsoft.com/workshop/java/cab-f.htm
Because Java applets often require the use of several class files, in addition to any image or data files that are required, the Cab system is especially useful with that language.
Using CABARC, a Java program can be assembled into a single Cabinet archive file with a .cab file extension.
This file would be placed on a Web site, and a new parameter is used with the <APPLET> tag to indicate the location of the archive.
The new parameter, cabbase, is shown in the following example of an <APPLET> tag:
<APPLET CODE="LearnToPolka.class" WIDTH=220 HEIGHT=500> <PARAM NAME="cabbase" VALUE="learn2polka.cab"> </APPLET>
If the cabbase parameter is found in an <APPLET> tag and the Web browser supports Cabinet files, cabbase will be used when the applet is loaded. If the cabbase parameter is not present, the CODE attribute of the <APPLET> tag will be used to indicate the applet's main class file. At the time of this writing, only Microsoft Internet Explorer 3.0 supports Cabinet files. Using cabbase does not interfere with the use of Java applets on browsers that do not support this archival system. A browser such as Netscape Navigator 2.02 would ignore the cabbase parameter and use the CODE and CODEBASE attribute to find the main Java class file to load.
This .cab file contains one or more files that are all downloaded together as a single compressed file. This Cabinet file should only include files that must be downloaded, and should not include any supporting programs such as dynamic link libraries (DLLs). These DLLs might already be present on the user's system, so they would be redundant to download in a Cabinet archive.
The most recent version of the CABARC tool operates in a manner similar to archival utilities such as PKZIP, LHArc, and others. It also supports the use of an information file that controls how the .cab file will be installed. This file has the .inf file extension, and it is included in the Cabinet archive.
As stated previously, COM software components such as ActiveX controls can be developed with several different programming languages. The most common choice, if Web pages on the subject are any indication, is Microsoft Visual C++. Using Visual C++, COM software can be written using one of three development libraries: the ActiveX Template Library, Microsoft Foundation Class Library, or the BaseCtl framework. ActiveX controls can use a variety of programming languages from Microsoft for component design in addition to Visual C++-Visual Basic, Visual J++, and even Word or Excel's programming languages.
ActiveX control programming is beyond the scope of this book, but if you're interested in developing your own controls, this section provides the information you need to get started.
Developing new controls for ActiveX requires the use of the ActiveX Software Development Kit (SDK) from Microsoft. It can be downloaded from the following Web page:
http://www.microsoft.com/intdev/sdk/
Using the ActiveX SDK requires the following software:
After downloading the ActiveX SDK, you end up with a single executable file, activex.exe, that will unpack itself. Instead of clicking the mouse to run this file, you need to use a command-line prompt or Run command from the Start menu to add an argument.
After you have moved the activex.exe to the root file directory in which you want it to be stored, you can unpack the file by entering the following:
activex.exe -d
You can then install the SDK by running the SETUPSDK.EXE program. Documentation for the development kit can be found in HTML form in the following subdirectory: \INetSDK\Help\Default.htm.
The following example demonstrates the fact that ActiveX technology has been with us for some time. You will take a Grid OCX file, which you can find on the CD-ROM, and add it into a Layout control, just like any other ActiveX control. The Grid control is normally used as a custom control in Visual Basic 4. Figure 21.4 shows the control you will use in the VB4 environment. If you have your own copy of VB4, you can use the grid32.ocx file from your windows/system directory.
Figure 21.4 : The grid32.ocx control in the VB4 development environment.
Using VB4 custom controls is not really recommended if you are building a Web page for public consumption. However, you might find that when building intranet applications in which you are trying to mirror the functionality of a current VB application, you could be forced into using specialized OCX files.
For a start, OCX files are somewhat "fatter" than their ActiveX brethren, increasing download time across the Web. OCX files do not have the Authentication and Security certification that ActiveX files have, and as you will see later in this example, this can cause alarm among users faced with what can appear to be a potential virus warning! Finally, although you will see how to include a CodeBase variable for the control, OCX files are best used from the local drive-in this case, from windows/system. If you are building an intranet application, it is very likely that the users of the application will already have the controls you include loaded onto their machine to enable the windows-based version of the application to operate.
So, now you are ready to start creating an application using a VB4 custom control.
OFF_RENT HEAT_LIGHT SALARIES VEHICLES JAN FEB MAR APR MAY
ColHeads(JAN)="January" ColHeads(FEB)="February" ColHeads(MAR)="March" ColHeads(APR)="April" ColHeads(MAY)="May" RowTitles(OFF_RENT)="Office Rent" RowTitles(HEAT_LIGHT)="Heat & Light" RowTitles(SALARIES)="Salaries" RowTitles(VEHICLES)="Vehicles" RowTitles(5)="TOTAL" DataVals(JAN,OFF_RENT) = 1000 DataVals(JAN,HEAT_LIGHT) = 70 DataVals(JAN,SALARIES) = 10000 DataVals(JAN,VEHICLES) = 500 DataVals(FEB,OFF_RENT) = 1000 DataVals(FEB,HEAT_LIGHT) = 80 DataVals(FEB,SALARIES) = 11100 DataVals(FEB,VEHICLES) = 500 DataVals(MAR,OFF_RENT) = 1000 DataVals(MAR,HEAT_LIGHT) = 80 DataVals(MAR,SALARIES) = 12000 DataVals(MAR,VEHICLES) = 600 DataVals(APR,OFF_RENT) = 1000 DataVals(APR,HEAT_LIGHT) = 70 DataVals(APR,SALARIES) = 12000 DataVals(APR,VEHICLES) = 600 DataVals(MAY,OFF_RENT) = 1000 DataVals(MAY,HEAT_LIGHT) = 60 DataVals(MAY,SALARIES) = 14000 DataVals(MAY,VEHICLES) = 600 OFF_RENT = 1 HEAT_LIGHT = 2 SALARIES = 3 VEHICLES = 4 JAN = 1 FEB = 2 MAR = 3 APR = 4 MAY = 5 Grid1.ColWidth(0) = 1000 Grid1.Row = 0 For x = 1 To 5 Grid1.ColWidth(x) = 700 Grid1.Col = x Grid1.Text = ColHeads(x) Next Grid1.Col = 0 For y = 1 To 5 Grid1.Row = y Grid1.Text = RowTitles(y) Next For x = 1 To 5 totval = 0 Grid1.Col = x For y = 1 To 4 Grid1.Row = y Grid1.Text = DataVals(x,y) totVal = totval + DataVals(x,y) Next Grid1.Row = 5 Grid1.Text = totVal Next
Figure 21.11: The security warning dialog.
The final result of all the steps is shown in Figure 21.12.
Figure 21.12: The VB4 Grid OCX control Web page.
ActiveX controls allow the creation of Web applications that mirror the functionality of Windows applications, with the added advantages of ease of distribution and, eventually, cross-platform usability.
Information Technology departments around the world are already gearing up to face the challenge of converting desktop windows applications into Web applications to be operated via the company's intranet infrastructure.
With the next version of MSIE just around the corner, in which the desktop and Web (whether via an intranet or Internet) become a seamless single platform, it is important that you understand how your Windows applications can be transformed into Web applications.
In this chapter, you have learned the basics of COM and ActiveX, and you've also seen how easy it is to treat OCX custom controls as though they are ActiveX controls-even if this is a stopgap measure until the control you require is made available as a fully Internet-aware ActiveX control.
Unless you feel the need to lie down in a darkened room for awhile, you can read the following chapters and find out how to put your newfound knowledge to good use:
I'm not sure what you mean by the statement that software components must follow a standard in binary form. What does this Component Object Model requirement mean by binary compatibility? | |
This means that the executable file (or files) associated with the component must fully support the COM standard. ActiveX controls, and other COM software, can be created with a number of different languages, so the source code of a COM object will vary widely from the source code of another COM object. However, the end result of those two COM objects will be highly similar, because the executable form of these components must follow the COM standard completely. This executable form is also called the binary form of a program, so ActiveX and COM feature binary compatibility. | |
Does ActiveX replace the Component Object Model? | |
ActiveX is a streamlined form of the COM standard intended for Internet and intranet use. It has several revisions from COM to make it more suitable for Internet programming, such as reduced file size to speed up remote downloading. It does not, however, fully supplant the COM as a component development solution. | |
Where can I find ActiveX controls to begin working with and learning from? | |
Microsoft maintains a gallery of sample controls, and there is also a C|Net Web site that features ActiveX news and a directory of controls. The Web addresses of these two sites are as follows:
http://www.microsoft.com/activex/gallery/ http://www.activex.com |